package com.example.doudou.demos.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.doudou.demos.utils.FileDfsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Author fangtao
 * @Date 2025 01 24 08 57
 * @Description
 **/
@RestController
@Api(tags = "文件处理")
public class FileController {
    @Resource
    private FileDfsUtil fileDfsUtil ;
    /**
     * 文件上传
     */
    @ApiOperation(value="上传图片并生成缩略图", notes="测试FastDFS图片上传并生成缩略图")
    @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
    public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){
        String result ;
        try{
            String path = fileDfsUtil.uploadByImage(file) ;
            if (!StringUtils.isEmpty(path)){
                result = path ;
            } else {
                result = "上传失败" ;
            }
        } catch (Exception e){
            e.printStackTrace() ;
            result = "服务异常" ;
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 文件上传
     */
    @ApiOperation(value="上传文件", notes="测试FastDFS文件上传")
    @RequestMapping(value = "/uploadByFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
    public ResponseEntity<String> uploadByFile (@RequestParam("file") MultipartFile file){
        String result ;
        try{
            String path = fileDfsUtil.uploadByFile(file) ;
            if (!StringUtils.isEmpty(path)){
                result = path ;
            } else {
                result = "上传失败" ;
            }
        } catch (Exception e){
            e.printStackTrace() ;
            result = "服务异常" ;
        }
        return ResponseEntity.ok(result);
    }
    /**
     * 文件删除
     */
    @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
    @ApiOperation(value="删除文件", notes="测试FastDFS文件删除")
    public ResponseEntity<String> deleteByPath (@RequestParam String filePathName){
        fileDfsUtil.deleteFile(filePathName);
        return ResponseEntity.ok("SUCCESS") ;
    }
}

